package com.hongdee.scm.cockpit.service.impl;

import com.hongdee.common.util.TokenUtil;
import com.hongdee.scm.cockpit.dao.ITScmCockpitDao;
import com.hongdee.scm.cockpit.service.ITScmCockpitService;
import jdk.nashorn.internal.parser.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/9/16.
 */
@Service
public class TScmCockpitServiceImpl implements ITScmCockpitService{

    @Autowired
    private ITScmCockpitDao itScmCockpitDao;

    /**
     * 车辆周报
     * @param uid
     * @return
     * @throws Exception
     */
    @Override
    public List carWeek(String uid) throws Exception{

        StringBuffer sql = new StringBuffer();
        List params = new ArrayList();
        String cid = TokenUtil.getCompanyId(uid);
        sql.append("select d.Query_Date,ifnull(sum(d.Finance_In),0) fin,ifnull(SUM(d.Finance_Out),0) fout,ifnull(SUM(d.Car_Out),0) cout ")
           .append("from t_scm_car_daily d where d.Query_Date BETWEEN DATE_SUB(CURDATE(),INTERVAL 6 day ) and CURDATE()");

        if(cid!=null&&!cid.equals("")){
            sql.append(" and d.company_id =?");
            params.add(cid);
        }

        sql.append(" group by d.Query_Date order by d.Query_Date");
        List list = this.itScmCockpitDao.list(sql.toString(),params.toArray());
        return list;
    }

    /**
     * 在途车辆
     * @param uid
     * @return
     * @throws Exception
     */
    @Override
    public List carOnWay(String uid) throws Exception{

        StringBuffer sql = new StringBuffer();
        List params = new ArrayList();
        String cid = TokenUtil.getCompanyId(uid);
        sql.append("select c.car_series,ifnull(count(1),0) as value from t_scm_car c " +
                "where c.status = '0' and c.finance_type ='0'");

        if(cid!=null&&!cid.equals("")){
            sql.append(" and c.company_id =?");
            params.add(cid);
        }
        sql.append(" group by c.car_series");
        List list = this.itScmCockpitDao.list(sql.toString(),params.toArray());
        return list;
    }

    /**
     *待确认出库
     * @param uid
     * @return
     * @throws Exception
     */
    @Override
    public Map carOutStandBy(String uid) throws Exception{

        StringBuffer sql1 = new StringBuffer(); //财务待出库
        StringBuffer sql2 = new StringBuffer(); //销售待出库
        StringBuffer sql3 = new StringBuffer(); //调拨待出库
        Map mapList = new HashMap();
        String[] mName = {"财务待出库","销售待出库","调拨待出库"};
        mapList.put("name",mName);
        String[] mCount= new String[3];
        String cid = TokenUtil.getCompanyId(uid);
        sql1.append("select count(o.id) as value from t_scm_sale_order_detail o " +
                "where o.status ='0' and not EXISTS ( " +
                "select w.car_id from t_scm_outwarehouse w where w.car_id = o.car_id) and o.company_id =? ");
        Map m1 = this.itScmCockpitDao.info(sql1.toString(),cid);
        if(m1.size()>0){
            mCount[0]=m1.get("value").toString();
        }else{
            mCount[0]="0";
        }
        sql2.append("select count(w.id) as value from t_scm_outwarehouse w where w.out_print_date is not null and out_status='0' and outType='1'" +
                " and EXISTS(select o.car_id from t_scm_sale_order_detail o where w.car_id = o.car_id and o.status ='0') and w.company_id=? ");
        Map m2 = this.itScmCockpitDao.info(sql2.toString(),cid);
        if(m2.size()>0){
            mCount[1]=m2.get("value").toString();
        }else{
            mCount[1]="0";
        }
        sql3.append("select count(w.id) as value from t_scm_outwarehouse w where w.out_print_date is not null and out_status='0' and outType='2'" +
                " and w.company_id=?");
        Map m3 = this.itScmCockpitDao.info(sql3.toString(),cid);
        if(m3.size()>0){
            mCount[2]=m3.get("value").toString();
        }else{
            mCount[2]="0";
        }
        mapList.put("count",mCount);
        return mapList;
    }

    /**
     * 库存提醒
     * @param uid
     * @return
     * @throws Exception
     */
    @Override
    public List carStockHint(String uid) throws Exception{

        StringBuffer sql = new StringBuffer();
        List params = new ArrayList();
        String cid = TokenUtil.getCompanyId(uid);

        sql.append("select * from( select trim(c.car_series) car_series,ifnull(t1.VALUE,0) a,ifnull(t2.value,0) b,ifnull(t3.value,0) c from t_scm_car c" +
                " left join ( select c.car_series,count(c.id) as value from t_scm_car c where c.status='1' and c.finance_type in('0','1')" +
                " and c.company_id=? group by c.car_series ) t1 on c.car_series = t1.car_series left join (" +
                " select c.car_series,count(o.id) as value from t_scm_sale_order_detail o left join t_scm_car c on c.id=o.car_id" +
                " where o.status ='0' and not EXISTS (select w.car_id from t_scm_outwarehouse w where w.car_id = o.car_id)" +
                " and o.company_id=? group by c.car_series ) t2 on c.car_series = t2.car_series left join (" +
                " select c.car_series,count(w.id) as value from t_scm_outwarehouse w left join t_scm_car c on c.id=w.car_id" +
                " where w.outType='1' and w.out_status='0' and EXISTS (select o.car_id from t_scm_sale_order_detail o where o.car_id = w.car_id and o.status ='0')" +
                " group by c.car_series ) t3 on c.car_series = t3.car_series where c.company_id =? group by c.car_series ) t" +
                " where t.a <> 0 or t.b <> 0 or t.c <> 0 ");
        if(cid!=null&&!cid.equals("")){
            params.add(cid);
            params.add(cid);
            params.add(cid);
        }

        sql.append(" group by t.car_series");
        List list = this.itScmCockpitDao.list(sql.toString(),params.toArray());
        return list;
    }
}
